package com.sphd.restaurantsystem.model;


import com.android.volley.VolleyError;
import com.sphd.restaurantsystem.info.SortInfo;
import com.sphd.restaurantsystem.interfaces.VolleyJsonObjectCallBack;
import com.sphd.restaurantsystem.presenter.FoodTypePresenterImpl;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Hu on 2016/11/14
 */

public class FoodTypeModelImpl implements FoodTypeModel, VolleyJsonObjectCallBack {
    FoodTypePresenterImpl foodTypePresenter;

    @Override
    public void sendUrl(String url, FoodTypePresenterImpl foodTypePresenter) {
        this.foodTypePresenter = foodTypePresenter;
        volleyUtil.setJsonObjectRequest(url, this);
    }

    @Override
    public void sendEdit(String s, FoodTypePresenterImpl foodTypePresenter) {
        this.foodTypePresenter=foodTypePresenter;
        volleyUtil.setJsonObjectRequest(s, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    foodTypePresenter.sendEditRes(response.getInt("res"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
foodTypePresenter.sendError(error);
            }
        });
    }



    @Override
    public void onResponse(JSONObject response) {
        try {
            List<SortInfo> sortInfos = JsonUtil.getListFromString(response.getString("res"),SortInfo.class);
            foodTypePresenter.sendRes(sortInfos);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        foodTypePresenter.sendError(error);
    }
}